<?php

namespace app\models;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii;
use EasyWeChat\Factory;
class Desk extends ActiveRecord
{
    public static function tableName()
    {
        return '{{desk}}';
    }

    /*生成桌面二维码*/
    public function createCode($code)
    {
        $config = [
            'app_id' => 'wx7ef81142f65fc806',
            'secret' => 'd2e7ee0037f223f2d7e3648cd2b0e3ed',
        ];

        $app = Factory::miniProgram($config);
        $we_re=$app->app_code->getUnlimit($code, array());
        #保存图片
        $filename = $we_re->saveAs(__DIR__.'/../web/qrcode/', $code.'.png');
        $code_url='/qrcode/'.$filename;
        return $code_url;
    }

    /*新建桌面*/
    public function createNew($mall_id,$name)
    {
        $code=md5(time().rand(10000,99999).rand(10000,99999));
        $in_data=array(
            'mall_id'=>$mall_id,
            'name'=>$name,
            'code'=>$code,
            'code_url'=>$this->createCode($code),
            'create_time'=>time(),
        );
        return Yii::$app->db->createCommand()->insert('desk',$in_data)->execute();
    }

    /*根据ID 删除桌面*/
    public function delOne($id)
    {
        #桌面旗下信息删除
        $msg_md=new Message();
        $msg_md->delOneByDeskId($id);
        #桌面删除
        $up_data=array(
            'is_del'=>'1',
        );
        $up_where=array(
            'id'=>$id,
        );
        return Yii::$app->db->createCommand()->update('desk',$up_data,$up_where)->execute();
    }

    /*根据店铺ID 删除桌面*/
    public function delOneByMallId($mall_id)
    {
        $desk_list=$this->getAllByMallId($mall_id);
        foreach ($desk_list as $desk)
        {
            $this->delOne($desk['id']);

        }
    }

    /*编辑桌面 名称*/
    public function editName($id,$name)
    {
        $up_data=array(
            'name'=>$name,
        );
        $up_where=array(
            'id'=>$id,
        );
        return Yii::$app->db->createCommand()->update('desk',$up_data,$up_where)->execute();
    }

    /*根据ID 获取单桌面*/
    public function getOneById($id)
    {
        $where=array(
            'id'=>$id
        );
        $info=(new Query())
            ->from('desk')
            ->where($where)
            ->one();
        if($info)
        {
            $mall_md=new Mall();
            $info['mall_info']=$mall_md->getOneById($info['mall_id']);
        }
        return $info;
    }

    /*根据CODE 获取单桌面*/
    public function getOneByCode($code)
    {
        $where=array(
            'code'=>$code
        );
        $info=(new Query())
            ->from('desk')
            ->where($where)
            ->one();
        if($info)
        {
            $mall_md=new Mall();
            $info['mall_info']=$mall_md->getOneById($info['mall_id']);
        }
        return $info;
    }

    /*获取所有桌面列表*/
    public function getAll($offset=null,$limit=null)
    {
        $offset=is_null($offset)?0:$offset;
        $limit=is_null($limit)?5:$limit;
        $list=(new Query())
            ->from('desk')
            ->where(array('is_del'=>'0'))
            ->orderBy('create_time DESC')
            ->offset($offset)
            ->limit($limit)
            ->all();
        if($list)
        {
            $out_list=array();
            $mall_md=new Mall();
            foreach ($list as $temp)
            {
                $temp['mall_info']=$mall_md->getOneById($temp['mall_id']);
                $out_list[]=$temp;
            }
            $list=$out_list;
        }

        return $list;
    }

    /*根据店铺ID 获取桌面列表*/
    public function getListByMallId($mall_id,$offset=null,$limit=null)
    {
        $offset=is_null($offset)?0:$offset;
        $limit=is_null($limit)?5:$limit;
        $where=array(
            'is_del'=>'0',
            'mall_id'=>$mall_id,
        );
        $list=(new Query())
            ->from('desk')
            ->where($where)
            ->orderBy('create_time DESC')
            ->offset($offset)
            ->limit($limit)
            ->all();
        if($list)
        {
            $out_list=array();
            $mall_md=new Mall();
            foreach ($list as $temp)
            {
                $temp['mall_info']=$mall_md->getOneById($temp['mall_id']);
                $out_list[]=$temp;
            }
            $list=$out_list;
        }

        return $list;
    }

    /*根据店铺ID 获取全部桌面列表*/
    public function getAllByMallId($mall_id)
    {
        $where=array(
            'is_del'=>'0',
            'mall_id'=>$mall_id,
        );
        $list=(new Query())
            ->from('desk')
            ->where($where)
            ->orderBy('create_time DESC')
            ->all();
        if($list)
        {
            $out_list=array();
            $mall_md=new Mall();
            foreach ($list as $temp)
            {
                $temp['mall_info']=$mall_md->getOneById($temp['mall_id']);
                $out_list[]=$temp;
            }
            $list=$out_list;
        }

        return $list;
    }

    /*获取桌面 计数*/
    public function getCount($where=null)
    {
        if(is_null($where))
        {
            $count=(new Query())
                ->from('desk')
                ->count();
        }
        else
        {
            $count=(new Query())
                ->from('desk')
                ->where($where)
                ->count();
        }

        return $count;

    }



}
